package com.huawei.hms.support.api.push;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.huawei.hms.aaid.constant.AaidIdConstant.PUSH_CLIENT_SELF_INFO;
import com.huawei.hms.support.api.push.c.a.c;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.ResourceLoaderUtil;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.RejectedExecutionException;

public final class PushReceiver extends BroadcastReceiver {

    public interface BoundKey {
        public static final String DEVICE_TOKEN_KEY = "deviceToken";
    }

    static class a implements Runnable {
        private Context a;
        private Intent b;

        private a(Context context, Intent intent) {
            this.a = context;
            this.b = intent;
        }

        @TargetApi(26)
        public void run() {
            Intent intent = new Intent("com.huawei.push.action.MESSAGING_EVENT");
            intent.setPackage(this.b.getPackage());
            Bundle bundle = new Bundle();
            bundle.putString("message_type", "received_message");
            bundle.putString("message_id", this.b.getStringExtra("msgIdStr"));
            bundle.putByteArray("message_body", this.b.getByteArrayExtra("msg_data"));
            bundle.putInt("inputType", 1);
            if (new com.huawei.hms.support.api.push.a.a().a(this.a, bundle, intent)) {
                HMSLog.i("PushReceiver", "receive " + this.b.getAction() + " and start service success");
            } else {
                HMSLog.e("PushReceiver", "receive " + this.b.getAction() + " and start service failed");
            }
        }
    }

    static class b implements Runnable {
        private Context a;
        private Intent b;

        private b(Context context, Intent intent) {
            this.a = context;
            this.b = intent;
        }

        public void run() {
            try {
                byte[] byteArrayExtra = this.b.getByteArrayExtra("device_token");
                if (byteArrayExtra == null) {
                    HMSLog.i("PushReceiver", "get a deviceToken, but it is null");
                    return;
                }
                HMSLog.i("PushReceiver", "receive a push token: " + this.a.getPackageName());
                com.huawei.hms.support.api.push.c.a.a.b bVar = new com.huawei.hms.support.api.push.c.a.a.b(this.a, PUSH_CLIENT_SELF_INFO.FILE_NAME);
                bVar.a("reqTokenTime", Long.valueOf(System.currentTimeMillis()));
                String str = new String(byteArrayExtra, "UTF-8");
                if (!str.equals(com.huawei.hms.support.api.push.c.a.b.a(this.a, PUSH_CLIENT_SELF_INFO.FILE_NAME))) {
                    HMSLog.i("PushReceiver", "receive a token, refresh the local token");
                    bVar.b("token_info");
                    com.huawei.hms.support.api.push.c.a.b.a(this.a, PUSH_CLIENT_SELF_INFO.FILE_NAME, str);
                }
                Intent intent = new Intent("com.huawei.push.action.MESSAGING_EVENT");
                intent.setPackage(this.b.getPackage());
                Bundle bundle = new Bundle();
                bundle.putString("message_type", "new_token");
                bundle.putString("device_token", str);
                if (!new com.huawei.hms.support.api.push.a.a().a(this.a, bundle, intent)) {
                    HMSLog.e("PushReceiver", "receive " + this.b.getAction() + " and start service failed");
                }
            } catch (UnsupportedEncodingException e) {
                HMSLog.e("PushReceiver", "encode token error");
            } catch (RejectedExecutionException e2) {
                HMSLog.e("PushReceiver", "execute task error");
            } catch (Exception e3) {
                HMSLog.e("PushReceiver", "handle push token error");
            }
        }
    }

    public void onToken(Context context, String str, Bundle bundle) {
        onToken(context, str);
    }

    public boolean onPushMsg(Context context, byte[] bArr, Bundle bundle) {
        String str = "";
        if (bundle != null) {
            str = bundle.getString(BoundKey.DEVICE_TOKEN_KEY);
        }
        onPushMsg(context, bArr, str);
        return true;
    }

    public void onPushMsg(Context context, byte[] bArr, String str) {
    }

    public void onToken(Context context, String str) {
    }

    public void onReceive(Context context, Intent intent) {
        if (intent != null && context != null) {
            HMSLog.i("PushReceiver", "push receive broadcast message, Intent:" + intent.getAction() + " pkgName:" + context.getPackageName());
            try {
                intent.getStringExtra("TestIntent");
                String action = intent.getAction();
                if (ResourceLoaderUtil.getmContext() == null) {
                    ResourceLoaderUtil.setmContext(context.getApplicationContext());
                }
                if ("com.huawei.android.push.intent.REGISTRATION".equals(action)) {
                    a(context, intent);
                } else if ("com.huawei.android.push.intent.RECEIVE".equals(action)) {
                    b(context, intent);
                } else {
                    HMSLog.i("PushReceiver", "message can't be recognised:" + intent.toUri(0));
                }
            } catch (Exception e) {
                HMSLog.e("PushReceiver", "intent has some error");
            }
        }
    }

    private void a(Context context, Intent intent) {
        try {
            if (intent.hasExtra("device_token")) {
                c.a().execute(new b(context, intent));
            } else {
                HMSLog.i("PushReceiver", "This message dose not sent by hwpush.");
            }
        } catch (RuntimeException e) {
            HMSLog.e("PushReceiver", "handlePushMessageEvent execute task runtime exception.");
        } catch (Exception e2) {
            HMSLog.e("PushReceiver", "handlePushTokenEvent execute task error");
        }
    }

    private void b(Context context, Intent intent) {
        try {
            if (intent.hasExtra("msg_data")) {
                c.a().execute(new a(context, intent));
            } else {
                HMSLog.i("PushReceiver", "This push message dose not sent by hwpush.");
            }
        } catch (RuntimeException e) {
            HMSLog.e("PushReceiver", "handlePushMessageEvent execute task runtime exception.");
        } catch (Exception e2) {
            HMSLog.e("PushReceiver", "handlePushMessageEvent execute task error");
        }
    }
}
